package com.one.pass.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ser.DefaultSerializerProvider;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.jms.Queue;
import java.util.TimeZone;

@Configuration
public class BaseConfig {
    // 读取配置文件中的应用名称
    @Value("${spring.application.name:''}")
    public String applicationName;

    // 定义一个ObjectMapper Bean，用于JSON序列化和反序列化
    @Bean
    public ObjectMapper initObjectMapper() {
        ObjectMapper jacksonMapper = new ObjectMapper();
        // 配置反序列化时忽略未知属性
        jacksonMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 配置序列化时忽略空的Bean（即没有任何属性的Bean）
        jacksonMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        DefaultSerializerProvider.Impl sp = new DefaultSerializerProvider.Impl();
        jacksonMapper.setSerializerProvider(sp);
        // 设置自定义的Null值序列化器
        sp.setNullValueSerializer(new JacksonUtil.NullSerializer());
        // 将日期序列化为时间戳
        jacksonMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
        // 设置时区为东八区
        jacksonMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return jacksonMapper;
    }

    // 定义一个RedisTemplate Bean，用于操作Redis
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        // 配置ObjectMapper的可见性
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 启用默认的类型检查
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();  // 设置完所有属性后，进行一些初始化工作
        return template;
    }

    // 定义一个ActiveMQ的队列Bean
    @Bean("${spring.application.name}.mq.try.run.send1")
    public Queue queue12() {
        return new ActiveMQQueue(applicationName + ".mq.try.run.send1");
    }
}
